var imgData = [];
var mp4Data = [];
setTimeout(function () {
  console.log(123123);
  console.log(window); 
  console.log(window.document);
  console.log(
    window.document.getElementsByClassName(
      "toolbar-logo toolbar-subMenu-box csdn-toolbar-fl"
    )
  );
  console.log(window.document.getElementsByTagName("img"));
  var html = window.document.body.innerHTML;

  console.log(html);
  chrome.runtime.onMessage.addListener(function (request, sender, sendMessage) {
    mp4Data = [];
    imgData = [];
    if (request.greeting == "html") {
      sendMessage(html);
    } else if (request.greeting == "imglist") {
      getImg(request.key, request.type, request.selectImg);
      imgData = [...new Set(imgData)];
      sendMessage(imgData);
    } else if (request.greeting == "scroll") {
      console.log("scroll", document.body.scrollHeight);
      window.scrollBy(0, 1500);
      getImg(request.key, request.type, request.selectImg);
      imgData = [...new Set(imgData)];
      sendMessage(imgData);
    } else if (request.greeting == "mp4") {
      getMp4();
      sendMessage(mp4Data);
    } else {
      sendMessage("FUCK OFF");
    }
  });
}, 1500);
function getImg(key, type, selectImg) {
  // type为关键词类型 class或者id
  // key为关键词
  if (key) {
    if (type == "class") {
      imglist = window.document.getElementsByClassName(key);
    } else if (type == "id") {
      imglist = window.document.getElementById(key).getElementsByTagName("img");
    }
  } else {
    imglist = window.document.getElementsByTagName("img");
  }

  for (var i = 0; i < imglist.length; i++) {
    if (selectImg) {
      if (imglist[i].src.indexOf(selectImg) > -1) {
        imgData.push(imglist[i].src);
      }
    } else {
      imgData.push(imglist[i].src);
    }
  }
  console.log(imgData);
}
function getMp4() {
  var mp4 = window.document.getElementsByTagName("video");
  for (var i = 0; i < mp4.length; i++) {
    console.log(mp4[i].src);
    mp4Data.push(mp4[i].src);
  }
}
